using System.Collections.Generic;
using UnityEngine;
using Yoozoo.Libs;
using Yoozoo.Managers;

namespace Gameplay.PVE.TurnBase
{
    public class SkillTemporarySystem : IClass
    {
        private BattleUnit unit;
        private List<SkillTemporary> skills;
        
        void IClass.OnReset()
        {
            skills?.Clear();
        }
        
        public void Init(BattleUnit unit)
        {
            this.unit = unit;
            skills = new List<SkillTemporary>();
        }
        
        public void OnTransmit(ETransmitType type,BaseTransmitArg arg)
        {
            if (type == ETransmitType.AddTemporarySkill)
            {
                var tempSkillArg = arg as TempSkillArg;
                var skill = ClassManager.Get<SkillTemporary>();
                skill.Init(tempSkillArg.source,tempSkillArg.skillId);
                skill.Start(unit);
                skills.Add(skill);
                //PveFlyTextManager.Instance.ShowFlyText(skill.config.name,unit.Data.position + new Vector3(0,0.5f,0),0.8f,0.3f,ETextColor.Yellow);
            }
        }

        public void Update()
        {
            for (int i = 0; i < skills.Count; i++)
            {
                var skill = skills[i];
                skill.Update();
                if (!skill.IsInAction())
                {
                    ClassManager.Free(skill);
                    skills.RemoveAt(i);
                    i--;
                }
            }
        }
    }
}